package com.example.tech34Base64;

import com.example.tech34Base64.utils.Base64Util;
import com.example.tech34Base64.utils.common.JSONRequest;
import com.example.tech34Base64.utils.common.JSONResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SendController {

    private static Logger LOGGER = LoggerFactory.getLogger(SendController.class);

    @RequestMapping("send")
    public Map<String, Object>  send(HttpServletRequest request, HttpServletResponse respons){
        Map<String, Object> paramMap = null;
        try {
            String msg = request.getParameter("msg");
            System.out.println("接收端：消息："+msg);

            List<Map<String, Object>> list = new ArrayList<>();
                //测试1
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", "abc.png");
                map.put("files",this.getBase64File("./file/abc.png"));
                list.add(map);
                //测试2
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("name", "def.docx");
                map2.put("files",this.getBase64File("./file/def.docx"));
                list.add(map2);
            paramMap = new HashMap<String, Object>();
            paramMap.put("data", list);
        }catch (Exception e){
            LOGGER.error("多线程：报错："+e,e);
        }
        return paramMap;
    }

    public List<String> getBase64File(String filsPaths) {
        List<String> list = new ArrayList<String>();
        try {
            if (null != filsPaths && !"".equals(filsPaths) ) {
                try {
                    String[] arrStringId = filsPaths.split(",");
                    for (String path : arrStringId) {
                        String fileBase64 = Base64Util.getBase64ByPath(path);
                        list.add(fileBase64);
                    }
                } catch (Exception e) {
                    LOGGER.error("附件转化base64出错：根据流：" + e.getMessage(), e);
                }
            }
        }catch (Exception e){
            LOGGER.error("附件转为为base64出错：根据流："+e.getMessage(),e);
        }
        return list;
    }

}
